#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int mod=998244353,inf=0x3f3f3f3f3f3f3f3f;
const int N=5e5+10,M=2e5+10;
mt19937 rnd(time(0));
int n;
int minx=inf,miny=inf;
int tot,ans;
struct info{
	int x,y;
}a[N];
int cmp1(info p,info q){return p.x==q.x?p.y<q.y:p.x<q.x;}
int cmp2(info p,info q){return p.y==q.y?p.x<q.x:p.y<q.y;}
vector<int>e[N];
int siz[N];
map<pair<int,int>,int>mp;
map<int,int>qe;
inline void add(int u,int v){e[u].push_back(v),e[v].push_back(u);}
void dfs(int x,int f)
{
	for ( auto v:e[x] )
	{
		if(v==f)continue;
		dfs(v,x);siz[x]+=siz[v];
		ans+=(n-siz[v])*siz[v];
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	cin >> n;
//	cerr << n << endl;
	for ( int i = 1 ; i <= n ; i++ )
	{
		cin >> a[i].x >> a[i].y;
//		cerr << a[i].x << " " << a[i].y << endl;
		minx=min(minx,a[i].x);
		miny=min(miny,a[i].y);
	}
//	cerr << n << endl;
	for ( int i = 1 ; i <= n ; i++ )
		a[i].x=a[i].x-minx+1,
		a[i].y=a[i].y-miny+1;
	sort(a+1,a+1+n,cmp1);
//	for ( int i = 1 ; i <= n ; i++ )cerr << a[i].x << " " << a[i].y << endl;
	for ( int i = 1 ; i <= n ; )
	{
		tot++;
		mp[{a[i].x,a[i].y}]=tot;
		qe.clear();
		int eid=mp[{a[i].x-1,a[i].y}];
		if(eid)
		{
			qe[eid]=1;
			add(eid,tot);
		}
		int sz=1;
		i++;
		for ( ; i<=n && a[i].x==a[i-1].x && a[i].y==a[i-1].y+1 ; i++ )
		{
			sz++;
			mp[{a[i].x,a[i].y}]=tot;
			eid=mp[{a[i].x-1,a[i].y}];
			if(eid)
			{
				if(!qe[eid])
				{
					qe[eid]=1;
					add(eid,tot);
				}
			}
		}
		siz[tot]=sz;
//		cerr<< tot << " " << sz << endl;
	}
	dfs(1,1);
	for ( int i = 1 ; i <= tot ; i++ )e[i].clear();
	tot=0;
	mp.clear();
	
	for ( int i = 1 ; i <= n ; i++ )swap(a[i].x,a[i].y);
	
	sort(a+1,a+1+n,cmp1);
	for ( int i = 1 ; i <= n ; )
	{
		tot++;
		mp[{a[i].x,a[i].y}]=tot;
		qe.clear();
		int eid=mp[{a[i].x-1,a[i].y}];
		if(eid)
		{
			qe[eid]=1;
			add(eid,tot);
		}
		int sz=1;
		i++;
		for ( ; i<=n && a[i].x==a[i-1].x && a[i].y==a[i-1].y+1 ; i++ )
		{
			sz++;
			mp[{a[i].x,a[i].y}]=tot;
			eid=mp[{a[i].x-1,a[i].y}];
			if(eid)
			{
				if(!qe[eid])
				{
					qe[eid]=1;
					add(eid,tot);
				}
			}
		}
		siz[tot]=sz;
	}
	dfs(1,1);
	cout << ans;
	return 0;
}


